<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/21/021
 * Time: 11:40
 */

namespace app\Lib\Model\Push;


use app\Lib\Exception\BlException;
use app\Lib\Model\WeiXin\GetWeiXinToken;

class WeiXinPush
{
    private $touser;
    private $template_id;
    private $url;
    private $appid;
    private $pagepath;
    private $data;
    private $access_token = '';

    /**
     * @param mixed $touser
     */
    public function setTouser($touser)
    {
//        dump($touser);
        $this->touser = $touser;
    }

    /**
     * @param mixed $template_id
     */
    public function setTemplateId($template_id)
    {
        $this->template_id = $template_id;
    }

    /**
     * @param mixed $url
     */
    public function setUrl($url)
    {
        $this->url = $url;
    }

    /**
     * @param mixed $appid
     */
    public function setAppid($appid)
    {
        $this->appid = $appid;
    }

    /**
     * @param mixed $pagepath
     */
    public function setPagepath($pagepath)
    {
        $this->pagepath = $pagepath;
    }

    /**
     * @param mixed $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }


    private function checkout(){
        if(empty($this->touser)){
            throw new BlException('接收者openid不能为空');
        }

        if(empty($this->template_id)){
            throw new BlException('推送模板不能为空');
        }
        if(empty($this->data)){
            throw new BlException('数据不能为空');
        }
        return true;
    }

    /**
     * @return mixed
     * @throws BlException
     */
    public function run(){
        $this->checkout();
        $result = (new GetWeiXinToken())->getAccessToken();
//        dump($result);
        $this->access_token =   $result['access_token'];
        $data['touser'] = $this->touser;
        $data['template_id'] = $this->template_id;
        if(!empty($this->url)){
            $data['url'] = $this->url;
        }
        if($this->appid){
            $data['miniprogram']['appid'] = $this->appid;
        }
        if($this->pagepath){
            $data['miniprogram']['pagepath'] = $this->pagepath;
        }
        $data['data'] = $this->data;
        $postUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->access_token;
        $curlPost = json_encode($data,JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        $params[CURLOPT_URL] = $postUrl;    //请求url地址
        $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $curlPost;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        $result =  json_decode($content,true);
        $recording = new WeiXinPushRecording();
        $recording->setTemplateId($this->template_id);
        $recording->setOpenId($this->touser);
        $recording->setStatus(1);
//        dump($result);
        if($result['errcode'] != 0){
            $recording->setStatus(0);
            $recording->setErrorMessage(json_encode($content));
        }
        $recording->setPushData(json_encode($data));
        $recording->run();
        return $content;

    }

}